import apiClient, { wrapApiCall } from './api';
import type {
  StoragePoolSummary,
  StoragePoolDetail,
  StorageVolumeSummary,
  StorageVolumeDetail,
  CreateVolumeRequest,
  ApiResponse,
  ListResponse,
} from '../types/api';

// 存储管理API服务
export const storageService = {
  // 获取所有存储池
  listStoragePools: async (connectionId: string): Promise<StoragePoolSummary[]> => {
    const response = await wrapApiCall<ListResponse<StoragePoolSummary>>(apiClient.get('/storage/pools', {
      params: { connection: connectionId }
    }));

    if (!response.pools) {
      throw new Error('存储池列表响应数据为空');
    }
    
    return response.pools;
  },

  // 获取存储池详情
  getStoragePoolDetail: async (connectionId: string, poolName: string): Promise<StoragePoolDetail> => {
    const response = await wrapApiCall<StoragePoolDetail>(apiClient.get('/storage/pool', {
      params: {
        connection: connectionId,
        pool: poolName
      }
    }));
    
    return response;
  },

  // 创建存储池
  createStoragePool: async (connectionId: string, xmlConfig: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/storage/pools', xmlConfig, {
      params: { connection: connectionId },
      headers: {
        'Content-Type': 'application/json',
      },
    }));
  },

  // 删除存储池
  deleteStoragePool: async (connectionId: string, poolName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.delete('/storage/pool', {
      params: { 
        connection: connectionId,
        pool: poolName 
      }
    }));
  },

  // 启动存储池
  startStoragePool: async (connectionId: string, poolName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/storage/pool/start', null, {
      params: { 
        connection: connectionId,
        pool: poolName 
      }
    }));
  },

  // 停止存储池
  stopStoragePool: async (connectionId: string, poolName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/storage/pool/stop', null, {
      params: { 
        connection: connectionId,
        pool: poolName 
      }
    }));
  },

  // 获取存储池中的所有卷
  listStorageVolumes: async (connectionId: string, poolName: string): Promise<StorageVolumeSummary[]> => {
    const response = await wrapApiCall<ListResponse<StorageVolumeSummary>>(apiClient.get('/storage/volumes', {
      params: { 
        connection: connectionId,
        pool: poolName 
      }
    }));

    if (!response.volumes) {
      throw new Error('存储卷列表响应数据为空');
    }
    
    return response.volumes;
  },

  // 获取存储卷详情
  getStorageVolumeDetail: async (connectionId: string, poolName: string, volumeName: string): Promise<StorageVolumeDetail> => {
    const response = await wrapApiCall<ApiResponse>(apiClient.get('/storage/volume', {
      params: { 
        connection: connectionId,
        pool: poolName,
        volume: volumeName 
      }
    }));
    
    if (!response.data) {
      throw new Error('存储卷详情响应数据为空');
    }
    
    return response.data as StorageVolumeDetail;
  },

  // 创建存储卷
  createStorageVolume: async (connectionId: string, poolName: string, data: CreateVolumeRequest): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/storage/volumes', data, {
      params: { 
        connection: connectionId,
        pool: poolName 
      }
    }));
  },

  // 删除存储卷
  deleteStorageVolume: async (connectionId: string, poolName: string, volumeName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.delete('/storage/volume', {
      params: { 
        connection: connectionId,
        pool: poolName,
        volume: volumeName 
      }
    }));
  },
};

export default storageService;